/*
 License
 
 Copyright © 2012 Kevin Kimmich
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights to 
 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 the Software, and to permit persons to whom the Software is furnished to do so, 
 subject to the following conditions:
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 @file
 Functions for use within the wwwserver library.
 */
#ifndef _http_internals_h
#define _http_internals_h

#ifdef __cplusplus
extern "C"
{
#endif	
	
	
#define DEFAULT_TIMEOUT FOREVER
	
	extern const result_code HTTP_HEADER_ERROR;
	extern const result_code HTTP_BODY_ERROR;
	
	/**
	 Removes leading and trailing white space from a string. It is removed in-place.
	 @param str The string that white space is removed from.
	 */
	void remove_white_space( char* str );
	
	/**
	 Parses an HTTP request header and populates the method appropriately.
	 @param ctx The connection context.
	 @param m The method.
	 */
	void parse_request_header(connection_context* ctx, method* m);
	
	/**
	 Parses an HTTP response header and populates the method appropriately.
	 @param ctx The connection context.
	 @param m The method.
	 */
	void parse_response_header(connection_context* ctx, method* m);
	
	/**
	 Initiaizes an HTTP request.
	 @param req The request.
	 */
	void request_init( request* req );
	
	/**
	 Cleanup an HTTP request.
	 @param req The request.
	 */
	void request_cleanup( request* req );
	
	/**
	 unescape a URL. Returns a pointer to the string. The caller must free the returned pointer.
	 @param url The url to unescape.
	 */
	char* unescape_url(const char* url);
	
	/**
	 Parse a URL into URI fields.
	 @param url The unescaped URL.
	 @param uri The URI structure. The caller should cleanup_uri() to free uri resources.
	 */
	int parse_url(const char* url, URI* uri);
	
	/**
	 Cleanup the memory allocated for the uri.
	 @param uri The uri.
	 */
	void cleanup_uri(URI* uri);
	
	/**
	 Internal function to process an in-process request.
	 @param h The server object.
	 @param ctx The connection context.
	 @param m The method.
	 @param req The request.
	 @param parse_header The header parsing function.
	 */
	void http_process_read(http_server_object* h,connection_context* ctx, method* m,request* req,void(*parse_header)(connection_context*, method*),int wait_time);
	
	/**
	 Initialize a method.
	 @param h The server object.
	 @param ctx The conntection context.
	 @param m The method.
	 @param t The context type. (CLIENT or SERVER)
	 */
	void method_init(http_server_object* h,connection_context* ctx, method* m,context_type t);
	
	/**
	 Get the mime type for the request.
	 @param m The method object.
	 */
	const char* get_mime_type_from_method(method* m);
	
	/**
	 Get the mime type from the filename.
	 */
	const char* get_mime_type_from_filename( char* filename );
	
	/**
	 Copy a NULL terminated string list.
	 */
	char** strlistcpy(const char** strs);

	/**
	 Get the rule matching the method. Returns NULL if there is no match.
	 */
	http_match_rule_t* get_match_rule(domain_object* d, method* m);

	
#ifdef __cplusplus
}
#endif	



#endif

